package io.modifier.basic.compose.widget.comm

import android.content.res.Configuration
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.modifier.basic.compose.theme.AppTheme
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.compose.theme.color

@Composable
fun ConfirmDlg(
    title: String = "",
    content: AnnotatedString,
    leftText: String,
    rightText: String,
    onLeftClick: () -> Unit = {},
    onRightClick: () -> Unit = {},
    leftColor: Color = AppThemeHolder.colors.appColor,
    rightColor: Color = AppThemeHolder.colors.appColor,
    titleColor: Color = AppThemeHolder.colors.txtColor,
    contentColor: Color = AppThemeHolder.colors.txtColor,
) {

    CenterColumn(
        Modifier
            .width(272.dp)
            .background(AppThemeHolder.colors.bgColor, AppThemeHolder.config.roundSmall)
    ) {

        V(18)

        Text(title, style = AppThemeHolder.textStyle.title.color(titleColor))

        V(12)

        Text(
            content, style = AppThemeHolder.textStyle.content.color(contentColor),
            textAlign = TextAlign.Center,
            modifier = Modifier.padding(horizontal = AppThemeHolder.config.gap)
        )

        V(19)

        ThisDivider(
            Modifier
                .height(1.dp)
                .fillMaxWidth()
        )

        CenterRow(Modifier.height(40.dp)) {

            TextBtn(
                leftText, modifier = Modifier
                    .weight(1f)
                    .fillMaxHeight(), onClick = onLeftClick, textColor = leftColor
            )

            ThisDivider(
                Modifier
                    .width(1.dp)
                    .fillMaxHeight()
            )

            TextBtn(
                rightText, modifier = Modifier
                    .weight(1f)
                    .fillMaxHeight(), onClick = onRightClick, textColor = rightColor
            )

        }
    }

}

@Composable
private fun PreviewDlg() {
    AppTheme {
        CenterColumn {
            ConfirmDlg(
                "Clear Messages",
                buildAnnotatedString {
                    append("Do you want to clear the group messages?")
                },
                leftText = "Clear",
                rightText = "Cancel",
            )
            V(16)


            ConfirmDlg(
                "Leave Group",
                buildAnnotatedString {
                    append("Do you want to leave the group ")
                    withStyle(style = SpanStyle(fontWeight = FontWeight(900))) {
                        append("The Avengers?")
                    }
                },
                titleColor = Color.Red,
                leftText = "Leave",
                leftColor = Color.Red,
                rightText = "Cancel",
            )
            V(16)


            ConfirmDlg(
                "Delete Group",
                buildAnnotatedString {
                    append("Do you want to delete the group ")
                    withStyle(style = SpanStyle(fontWeight = FontWeight(900))) {
                        append("The Avengers ")
                    }
                    append("and all of its messages for all members?")
                },
                leftText = "Delete",
                leftColor = Color.Red,
                rightText = "Cancel",
            )
            V(16)

        }
    }
}

@Preview(backgroundColor = 0xFF121212, showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Composable
private fun PreviewLightDlgs() {
    PreviewDlg()
}

@Preview(backgroundColor = 0xFFf5f5f5, showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun PreviewDarkDlgs() {
   PreviewDlg()
}
